<div id="settingsHolidays" style="height:100%">
    <div class="ui-layout-west" id="settingsHolidaysFixedDates">
        <div class="ui-layout-center">
            <div class="clearfix tbl settingsHolidaysFixedDatesTable" id="settingsHolidaysFixedDatesTable"></div>    
        </div>
        <div class="ui-layout-south">
            <button class="small" id="hFixedAdd">Add new</button>    
            <button class="small" id="hFixedDelete">Delete</button>    
            <button class="small" id="hFixedSave">Save</button>    
            <button class="small" id="hFixedCancel">Cancel</button>
        </div>
    </div>
    <div class="ui-layout-center" id="settingsHolidaysCustom">
        <div class="ui-layout-center">
            <div class="clearfix tbl settingsHolidaysCustomTable" id="settingsHolidaysCustomTable"></div>    
        </div>
        <div class="ui-layout-south">
            <button class="small" id="hCostumAdd">Add new</button>    
            <button class="small" id="hCostumDelete">Delete</button>    
            <button class="small" id="hCostumSave">Save</button>    
            <button class="small" id="hCostumCancel">Cancel</button>
        </div>
    </div>
</div>
<script>
    var SettingsHolidaysFX = function() {
        var _self = this;
        _self.perform = function() {
            //dodavanje layout-a za parent div
            $("#settingsHolidays").layout({
                resizable: false,
                closable: false,
                west__size: ".5"
            });
            $("#settingsHolidaysFixedDates").layout({
                resizable: false,
                closable: false,
                south__size: "50"
            });
            $("#settingsHolidaysCustom").layout({
                resizable: false,
                closable: false,
                south__size: "50"
            });
            $("#settingsHolidaysFixedDatesTable").addScroll();
            _self.loadDataForSettingsHFDTable();
            $("#settingsHolidaysCustomTable").addScroll();
            _self.loadDataForSettingsHCTable();

            window.refreshAndSortTable = _self.refreshSortedTable;
        };
        _self.refreshSortedTable = function(table, columnName, ascDesc, callback){
            var _self = this;
            _self.table = table;
            _self.request = 0;

            if(table == "settingsHolidaysFixedDatesTable")
                _self.request = 600;

            if(table == "settingsHolidaysCustomTable")
                _self.request = 601;

            _self.columnName = columnName;
            _self.sortItem = "";
            _self.ascDesc = ascDesc;
            _self.action = "getItems";

            _self.random = Math.random();
            _self.url = "js/tableParams/" + _self.request + ".json?x=" + random;

            $.get(_self.url, function(response) {
                $(response.desc).each(function(index,value){
                    if(value.title == columnName){
                        _self.sortItem = value.param;
                        return false;
                    }
                });

                var params = {
                    request: _self.request,
                    action: _self.action,
                    sortItem: _self.sortItem,
                    ascDesc: _self.ascDesc
                };

                window.tableConstructor.generateTable(params, callback);
            });

        };
        _self.loadDataForSettingsHFDTable = function(callback) {
            var params = {
                request: 600,
                action: "getItems"
            };
            window.tableConstructor.generateTable(params, callback);
        };
        _self.loadDataForSettingsHCTable = function(callback) {
            var params = {
                request: 601,
                action: "getItems"
            };
            window.tableConstructor.generateTable(params, callback);
        };

        _self.assignEventHandlers = function(){
            $(document).on("click", ".scrollable .tbl.settingsHolidaysFixedDatesTable .row.data", function() {
                window.backendInterface.editItem($('.scrollable .tbl.settingsHolidaysFixedDatesTable'),$(this),_self.loadDataForSettingsHFDTable,600);
            });

            $(document).on("click", "#hFixedAdd", function() {
                window.backendInterface.addItem($('.scrollable .tbl.settingsHolidaysFixedDatesTable'),_self.loadDataForSettingsHFDTable,600);
            });
            $(document).on("click", "#hFixedDelete", function() {
                window.backendInterface.deleteItem(600);
            });
            $(document).on("click", "#hFixedSave", function() {
                window.backendInterface.save(600);
            });
            $(document).on("click", "#hFixedCancel", function() {
                window.backendInterface.cancel(600);
            });

            $(document).on("click", ".scrollable .tbl.settingsHolidaysCustomTable .row.data", function() {
                window.backendInterface.editItem($('.scrollable .tbl.settingsHolidaysCustomTable'),$(this),_self.loadDataForSettingsHCTable,601);
            });

            $(document).on("click", "#hCostumAdd", function() {
                window.backendInterface.addItem($('.scrollable .tbl.settingsHolidaysCustomTable'),_self.loadDataForSettingsHCTable,601);
            });
            $(document).on("click", "#hCostumDelete", function() {
                window.backendInterface.deleteItem(601);
            });
            $(document).on("click", "#hCostumSave", function() {
                window.backendInterface.save(601);
            });
            $(document).on("click", "#hCostumCancel", function() {
                window.backendInterface.cancel(601);
            });
        };
        _self.clearEventHandlers = function(){
            $(document).off("click", ".scrollable .tbl.settingsHolidaysFixedDatesTable .row.data");
            $(document).off("click", ".scrollable .tbl.settingsHolidaysCustomTable .row.data");

            $(document).off("click", "#hFixedAdd");
            $(document).off("click", "#hFixedDelete");
            $(document).off("click", "#hFixedSave");
            $(document).off("click", "#hFixedCancel");

            $(document).off("click", "#hCostumAdd");
            $(document).off("click", "#hCostumDelete");
            $(document).off("click", "#hCostumSave");
            $(document).off("click", "#hCostumCancel");
        };
    };
    if(!window.instance){
        window.instance = new SettingsHolidaysFX();
        window.instance.perform();
        window.instance.clearEventHandlers();
        window.instance.assignEventHandlers();
    }
    else{
        window.instance = new SettingsHolidaysFX();
        window.instance.perform();
        window.instance.clearEventHandlers();
        window.instance.assignEventHandlers();
    }
</script>